# coding=utf-8
'''create by pbc '''

import json
import uuid
from django.shortcuts import HttpResponse
from dockerManagerApp import models


def user_logout(request):
    request.session['django_username'] = ""
    request.session.set_expiry(1)
    ret = {'status': True, 'error': None, 'data': None}
    res = HttpResponse(json.dumps(ret))
    # res = render(request, 'login.html')
    return res


def user_validate(request):
    ret = {'status': True, 'error': None, 'data': None}
    u = request.POST.get("username")
    p = request.POST.get("password")

    # 查询数据库 -top
    # db_u = models.UserInfo.objects.filter(username=u)
    # db_p = models.UserInfo.objects.filter(password=p)
    # print(db_u,db_p)
    # 查询数据库 -end

    # models.TerminalManager.objects.create(terminalSerialNum=n,
    #                                       terminalRegTime=t,
    #                                       terminalStatus="1",  # 默认情况下为1 离线状态
    #                                       terminalHardwareInfo=h,
    #                                       terminalSoftwareInfo=s
    #                                       )
    #
    # listValue = models.TerminalManager.objects.filter(terminalSerialNum=n).values('terminalSerialNum')
    # for item in listValue:
    #     if item.get('terminalSerialNum') == n:
    #         '''第二次查询之后，如果查询到了，说明记录保存成功'''
    #         ret = {'status': True, 'error': None, 'data': None}
    #         res = HttpResponse(json.dumps(ret))
    #         return res
    #
    # ret = {'status': False, 'error': ERROR_CODE_001, 'data': None}
    # res = HttpResponse(json.dumps(ret))
    # return res

    listValue = models.UserInfo.objects.all().values('username')
    if len(listValue) == 0:
        '''代表用户数据库里面没有任何信息，那么就给它初始化一个用户信息'''
        models.UserInfo.objects.create(uuid=uuid.uuid1(),
                                       username='sysadmin',
                                       password='admin==1',
                                       email='159@163.com',
                                       other='初始化数据库创建的第一个默认管理员用户')

        listValue = models.UserInfo.objects.filter(username=u).values('username', 'password')
        for item in listValue:
            if item.get('username') == "sysadmin" and item.get('password') == 'admin==1':
                '''说明初始化成功了'''
                break
        ret = {'status': False, 'error': '由于这是第一次登录初始化数据库的时候失败了', 'data': None}
        return HttpResponse(json.dumps(ret))

    if u == 'superuseradmin' and p == 'admin==1':
        ret['data'] = u
        ret['status'] = True
        ret['error'] = None

        res = HttpResponse(json.dumps(ret))
        res.set_cookie('django_username', u)
        request.session['django_username'] = u
        request.session.set_expiry(600)
        return res

    listValue = models.UserInfo.objects.filter(username=u).values('username', 'password')
    for item in listValue:
        if item.get('username') == u and item.get('password') == p:
            ret = {'status': True, 'error': None, 'data': None}
            res = HttpResponse(json.dumps(ret))
            res.set_cookie('django_username', u)
            request.session['django_username'] = u
            request.session.set_expiry(600)
            return res
    ret['data'] = u
    ret['status'] = False
    ret['error'] = '登录失败'

    return HttpResponse(json.dumps(ret))
